import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import _ from 'lodash'
// import HomeView from '../views/HomeView.vue'

const Login = () => import('@/views/Login/Login.vue');
const Home = () => import('@/views/Home/Home.vue');
const Sign = () => import('@/views/Sign/Sign.vue');
const Exception = () => import('@/views/Exception/Exception.vue');
const Apply = () => import('@/views/Apply/Apply.vue');
const Check = () => import('@/views/Check/Check.vue');

// const store = import('@/store')
import type {StateAll} from '@/store'
import store from "@/store";
// 路由规则
declare module 'vue-router' {
    interface RouteMeta {
        // ？可选项
        menu?: boolean
        title?: string
        icon?: string
        auth?: boolean
    }
}

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'home',
        component: Home,
        redirect: '/sign',
        meta: {
            menu: true,
            title: '考勤管理',
            icon: 'document-copy',
            auth: true
        },
        children: [
            {
                path: 'sign',
                name: 'sign',
                component: Sign,
                meta: {
                    menu: true,
                    title: '在线打卡签到',
                    icon: 'calendar',
                    auth: true
                },
                beforeEnter(to, from, next) {
                    const usersInfos = (store.state as StateAll).users.infos
                    const singsInfos = (store.state as StateAll).signs.infos
                    // 如果打卡信息为空，则请求数据
                    if (_.isEmpty(singsInfos)) {
                        store.dispatch('signs/getTime', {userid: usersInfos._id}).then(res => {
                            // console.log(usersInfos._id)
                            if (res.data.errcode === 0) {
                                store.commit('signs/updateInfos', res.data.infos)
                                console.log(res.data+'123')
                                next()

                            }
                        })


                    } else {
                        next()

                    }
                    // console.log(infos._id);
                }
            },
            {
                path: 'exception',
                name: 'exception',
                component: Exception,
                meta: {
                    menu: true,
                    title: '异常考勤查询',
                    icon: 'warning',
                    auth: true,
                }
            },
            {
                path: 'apply',
                name: 'apply',
                component: Apply,
                meta: {
                    menu: true,
                    title: '添加考勤审批',
                    icon: 'document-add',
                    auth: true,
                }
            },
            {
                path: 'check',
                name: 'check',
                component: Check,
                meta: {
                    menu: true,
                    title: '我的考勤审批',
                    icon: 'finished',
                    auth: true,
                }
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: Login
    }
// ,
// {
//   path: '/about',
//   name: 'about',
//   // route level code-splitting
//   // this generates a separate chunk (about.[hash].js) for this route
//   // which is lazy-loaded when the route is visited.
//   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
// }
]


const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach((to, from, next) => {
    const token = (store.state as StateAll).users.token
    const infos = (store.state as StateAll).users.infos
    // 需要权限
    if (to.meta.auth && _.isEmpty(infos)) {
        // 有权限
        if (token) {
            store.dispatch('users/infos').then((res) => {
                if (res.data.errcode === 0) {
                    store.commit('users/updateInfos', res.data.infos)
                    next()
                }
            });
        } else {
            next('/login');
        }

    } else {
        // 不需要权限
        if (token && to.path === '/login') {
            // 有权限，跳转首页
            next('/');
        } else {
            // 没有权限放行
            next();
        }
    }
})

export default router
